package com.example.cheeseerpadmin.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusProvider;
import com.example.cheeseerpadmin.bus.service.BusGoodsService;
import com.example.cheeseerpadmin.bus.service.BusProviderService;
import com.example.cheeseerpadmin.bus.vo.GoodsVo;
import com.example.cheeseerpadmin.sys.common.*;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cheese
 * @since 2022-02-14
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    public BusGoodsService busGoodsService;
    @Autowired
    public BusProviderService busProviderService;

    /**
     * 加载商品数据和模糊查询功能
     *
     * @param busGoods
     * @return
     */
    @RequestMapping("/loadAllGoods")
    public DataGridView loadAllGoods(BusGoods busGoods) {
        Page<BusGoods> busGoodsPage = new Page<>();
        QueryWrapper<BusGoods> busGoodsQueryWrapper = new QueryWrapper<>();
        busGoodsQueryWrapper.like(StringUtils.isNotBlank(busGoods.getGoodsname()), "goodsname", busGoods.getGoodsname());
        busGoodsQueryWrapper.like(StringUtils.isNotBlank(busGoods.getProductcode()), "productcode", busGoods.getProductcode());
        busGoodsQueryWrapper.like(StringUtils.isNotBlank(busGoods.getPromitcode()), "promitcode", busGoods.getPromitcode());
        busGoodsQueryWrapper.like(StringUtils.isNotBlank(busGoods.getDescription()), "description", busGoods.getDescription());
        busGoodsQueryWrapper.like(StringUtils.isNotBlank(busGoods.getSize()), "size", busGoods.getSize());
        busGoodsQueryWrapper.eq(busGoods.getProviderid() != null, "providerid", busGoods.getProviderid());
        Page<BusGoods> page = busGoodsService.page(busGoodsPage, busGoodsQueryWrapper);
        List<BusGoods> records = page.getRecords();
        for (BusGoods busGoods1 : records) {
            Integer providerid = busGoods1.getProviderid();
            BusProvider busProvider = busProviderService.getById(providerid);
            busGoods1.setProvidername(busProvider.getProvidername());
        }
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 加载供应商数据
     *
     * @return
     */
    @RequestMapping("/loadProviderTreeJson")
    public DataGridView loadProviderTreeJson() {
        List<BusProvider> list = busProviderService.list();
        ArrayList<TreeNode> treeNodes = new ArrayList<>();
        for (BusProvider busProvider : list) {
            TreeNode treeNode = new TreeNode(busProvider.getId(), 0, busProvider.getProvidername(), false);
            treeNodes.add(treeNode);
        }
        return new DataGridView(treeNodes);
    }

    /**
     * 加载供应商数据，但不是提供给树，而是提供给select标签元素
     *
     * @return
     */
    @RequestMapping("/loadAllProviderForSelect")
    public DataGridView loadAllProviderForSelect() {
        DataGridView dataGridView = new DataGridView();
        QueryWrapper<BusProvider> busProviderQueryWrapper = new QueryWrapper<>();
        busProviderQueryWrapper.eq("available", Constant.AVAILABLE_TRUE);
        List<BusProvider> list = busProviderService.list(busProviderQueryWrapper);
        dataGridView.setData(list);
        return dataGridView;
    }

    /**
     * 添加商品
     *
     * @param busGoods
     * @return
     */
    @RequestMapping("/addGoods")
    public ResultObj addGoods(BusGoods busGoods) {
        try {
            if (busGoods.getGoodsimg() != null && busGoods.getGoodsimg().endsWith("_temp")) {
                String newName = WebFileUtils.renameFile(busGoods.getGoodsimg());
                busGoods.setGoodsimg(newName);
            }
            busGoodsService.save(busGoods);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新商品
     *
     * @param busGoods
     * @return
     */
    @RequestMapping("/updateGoods")
    public ResultObj updateGoods(BusGoods busGoods) {
        try {
            //说明不是默认图片。因为默认图片是在另外一个文件当中，理应当不做处理的
            if (!(busGoods.getGoodsimg() != null && busGoods.getGoodsimg().equals(Constant.IMAGES_DEFAULTGOODSIMG_PNG))) {
                if (busGoods.getGoodsimg().endsWith("_temp")) {
                    String newName = WebFileUtils.renameFile(busGoods.getGoodsimg());
                    busGoods.setGoodsimg(newName);
                    //删除原先的图片
                    String oldPath = busGoodsService.getById(busGoods.getId()).getGoodsimg();
                    if (!oldPath.equals(Constant.IMAGES_DEFAULTGOODSIMG_PNG)) {
                        WebFileUtils.removeFileByPath(oldPath);
                    }
                }
            }
            busGoodsService.updateById(busGoods);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 商品批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchDeleteGoods")
    public ResultObj batchDeleteGoods(Integer[] ids, String[] goodsimgs) {
        try {
            Collection<Serializable> idList = new ArrayList<>(Arrays.asList(ids));
            busGoodsService.removeByIds(idList);
            WebFileUtils.removeFileByPaths(goodsimgs);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 商品删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteGoodsById")
    public ResultObj deleteGoodsById(Integer id, String goodsimg) {
        try {
            WebFileUtils.removeFileByPath(goodsimg);
            busGoodsService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }

    }
}

